//
//  CityStreetViewController.m
//  cityStar
//
//  Created by wangjia on 14/12/14.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "CityStreetViewController.h"
#import "CommityListViewController.h"
#import "CityStreetCell.h"
#import "HTDefine.h"
#import "SVPullToRefresh.h"

NSString *const CityStreetCellID = @"CityStreetCellID";


@interface CityStreetViewController ()

@property (nonatomic,weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrayRequest;
@property (nonatomic, assign) int page;

@end

@implementation CityStreetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"街尚生活";
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 30)];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(-30, 0, 90, 30)];
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [leftButton setTitleColor:[UIColor colorWithWhite:0.200 alpha:1.000] forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor colorWithRed:0.991 green:0.532 blue:0.303 alpha:1.000] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"back_touch"] forState:UIControlStateHighlighted];
    
    [view addSubview:leftButton];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.navigationItem.leftBarButtonItem = barButtonItem;
    [self setUpTableViews];
    
    self.arrayRequest = [[NSMutableArray alloc]init];

    __weak typeof(self)weakSelf = self;
    self.arrayRequest = [[NSMutableArray alloc]init];
//    [self.tableView addPullToRefreshWithActionHandler:^{
//        self.page = 1;
        [weakSelf webRequest_GetCityStreetMore:NO];
//    }];
//    [self.tableView addInfiniteScrollingWithActionHandler:^{
//        [weakSelf webRequest_GetCityStreetMore:YES];
//    }];
//    [self.tableView triggerPullToRefresh];
}

- (void)webRequest_GetCityStreetMore:(BOOL)isMore
{
    NSString *postUrl = [NSString stringWithFormat:@"%@%@",KinterFace_RegionLive,[KStandUserDefaults objectForKey:KUserDefaultKey_PersonalCityID]?[KStandUserDefaults objectForKey:KUserDefaultKey_PersonalCityID]:@"122"];
//    NSDictionary *dicy = @{@"city":@"122"};
    
    [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            if (isMore) {
                [self.arrayRequest addObjectsFromArray:response[@"value"]];
                self.page = self.page + 1;
            }else{
                self.arrayRequest = [[NSMutableArray alloc]initWithArray:response[@"value"]];
            }
            [self.tableView reloadData];

        }
        if (isMore) {
            [self.tableView.infiniteScrollingView stopAnimating];
        }else{
            [self.tableView.pullToRefreshView stopAnimating];
        }
    }];
}

-(void)setUpTableViews
{
    [self.tableView registerNib:[CityStreetCell nib] forCellReuseIdentifier:CityStreetCellID];
}
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [CityStreetCell heightOfTableCell];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayRequest.count;
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CityStreetCell *ListdetailCell = [tableView dequeueReusableCellWithIdentifier:CityStreetCellID];
    ListdetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
    CityStreetModel *model = [CityStreetModel objectFromDictionary:self.arrayRequest[indexPath.row]];
    [ListdetailCell setDataModel:model];
    return ListdetailCell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView reloadData];
    NSLog(@"%li",(long)indexPath.row);
    CityStreetModel *model = [CityStreetModel objectFromDictionary:self.arrayRequest[indexPath.row]];

    CommityListViewController *commitylistVC = [[CommityListViewController alloc]initWithDistrictID:model.objectId andDistrictName:model.name];
    [self.navigationController pushViewController:commitylistVC animated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0) {
        self.navigationController.navigationBar.translucent = NO; //禁止半透明毛玻璃效果
        self.navigationController.navigationBar.tintColor = kWhiteNavTitleColor; //返回按钮颜色
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor]; //bar背景颜色
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kWhiteNavTitleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}; //标题颜色
        self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    }else{
        [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]]; //bar背景颜色
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kWhiteNavTitleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
        self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
